package com.qdlc.p2p.biz.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.qdlc.p2p.biz.service.BorrowConfigService;
import com.qdlc.p2p.common.exception.BusinessException;
import com.qdlc.p2p.dal.common.model.jpa.PageDataList;
import com.qdlc.p2p.dal.common.model.jpa.QueryParam;
import com.qdlc.p2p.dal.dao.BorrowConfigDao;
import com.qdlc.p2p.dal.dto.BorrowConfig;
import com.qdlc.p2p.dal.model.BorrowConfigModel;

@Service("borrowConfigService")
public class BorrowConfigServiceImpl implements BorrowConfigService {

    @Resource
    private BorrowConfigDao borrowConfigDao;

    @Override
    public List<BorrowConfig> findAll() {
        return borrowConfigDao.findAll();
    }

    @Override
    public PageDataList<BorrowConfigModel> list(BorrowConfigModel model) {
        PageDataList<BorrowConfig> plist = borrowConfigDao.list(model);
        PageDataList<BorrowConfigModel> pageDataList = new PageDataList<BorrowConfigModel>();
        List<BorrowConfigModel> list = new ArrayList<BorrowConfigModel>();
        pageDataList.setPage(plist.getPage());
        if (plist.getList().size() > 0) {
            for (int i = 0; i < plist.getList().size(); i++) {
                BorrowConfig c = plist.getList().get(i);
                BorrowConfigModel cm = BorrowConfigModel.instance(c);
                list.add(cm);
            }
        }
        pageDataList.setList(list);

        return pageDataList;
    }

    @Override
    public BorrowConfig find(long id) {
        return borrowConfigDao.find(id);
    }

    @Override
    public void add(BorrowConfig borrowConfig) {
        borrowConfigDao.save(borrowConfig);
    }

    @Override
    public void update(BorrowConfig borrowConfig) {
        borrowConfigDao.update(borrowConfig);
    }

    @Override
    public List<BorrowConfig> findAllNotFlowAndSecond() {
        return borrowConfigDao.findAllNotFlowAndSecond();
    }

    @Override
    public void checkNid(String cname) {
        QueryParam param = QueryParam.getInstance();
        param.addParam("cname", cname.trim());
        List<BorrowConfig> list = borrowConfigDao.findByCriteria(param);
        if (list.size() != 0) {
            throw new BusinessException("该标识已存在", BusinessException.TYPE_JSON);
        }
    }
}
